//
//  HMStatusListViewModel.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/10.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit

class HMStatusListViewModel: NSObject {

    
//    static let shareStatuslist = HMStatusListViewModel()
    
    var statuslist : [HMstatusModel]?
    
    lazy var useraccout : HMusercount = HMusercount.shareuserCount
    
    func loadDataFortoken(ispoUP :Bool, compelet : (isSucess : Bool,count : Int)->()){
    
        var maxId = ispoUP == true ? (statuslist?.last?.hmstatus?.id ?? 0) : 0
        
        if maxId > 0 {
        
            maxId -= 1
        }
        
        let sinId = ispoUP == false ?  (statuslist?.first?.hmstatus?.id ?? 0) : 0
        
        HMhttpSession.sharanetWork.loadStatus(useraccout.accessToken!,maxId: maxId,sinceId:sinId) { (response, error) -> () in
            if error != nil
            {
                print("请求失败")
                compelet(isSucess: false,count: 0)
            }
            guard let result = response as? [String : AnyObject]  else
            {
                    print("这不是一个字典")
                   return
            }
            guard let statusDict = result["statuses"] as? [[String:AnyObject]] else
            {
                print("这个字典里没有status")
                return
            }
            var tempArray = [HMstatusModel]()
            
            for value in statusDict
            {
                let status = HMStatus(dict: value)
                tempArray.append(HMstatusModel(status:status))
            }
       if  self.statuslist == nil
        {
        
            self.statuslist = [HMstatusModel]()
        }
            
        if ispoUP
        {
            self.statuslist = self.statuslist! + tempArray
        }
        else
        {
            self.statuslist = tempArray + self.statuslist!
        }
        compelet(isSucess: true,count: (tempArray.count))
        }
    }
    
    
}
